//
//  UIGridView.m
//  LotteryClient
//
//  Created by Sunyon 14-5-7.
//  Copyright (c) 2014年 CTTC. All rights reserved.
//

#import "UIGridView.h"
#import "UITableView+Refresh.h"
#import "UIGridViewCell.h"

@interface UIGridView () <UITableViewDelegate>
@end

@implementation UIGridView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
	{
        // Initialization code
        _intervalInCells = 1;

        _tableView = [[UITableView alloc] initWithFrame:CGRectZero
                                                  style:UITableViewStylePlain];

        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor clearColor];
		_tableView.delegate = self;
		_tableView.dataSource = self;
		self.autoresizesSubviews = YES;

        [self addSubview:_tableView];

    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    _tableView.frame = CGRectMake(0, 0, self.width, self.height - _intervalInCells);
}


#pragma mark UITable datasource and delegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_delegate numberOfRowsInGridView];
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil)
	{
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
									  reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor clearColor];

    }
	
	[cell.contentView removeAllSubviews];

	NSInteger count = [_delegate numberOfColumnInGridView];
	float width = (self.frame.size.width - (count +1)*_intervalInCells) / count ;
	float height = [_delegate heightOfRowInGridView:indexPath.row]  ;

	for(int i = 0;i < count; i++)
	{
		UIGridViewCell *gridCell = [_delegate cellInGridViewAtRow:indexPath.row
														   column:i];
		gridCell.backgroundColor = [UIColor clearColor];
		if (gridCell)
		{
			[gridCell willChangeValueForKey:@"self.bounds"];

			gridCell.frame = CGRectMake((1 + i) * _intervalInCells + width *(i) , _intervalInCells,
                                         width,
                                         height - _intervalInCells);
//            gridCell.center = CGPointMake((1 + i) * _intervalInCells + width *( 0.5 + i) , _intervalInCells + height * 0.5);
			[gridCell didChangeValueForKey:@"self.bounds"];
			gridCell.row = indexPath.row;
			gridCell.column = i ;
			gridCell.index = indexPath.row * count + i;
			[gridCell addTarget:self
                         action:@selector(cellClick:)
               forControlEvents:UIControlEventTouchUpInside];
			[cell.contentView addSubview:gridCell];
		}
	}

    return cell ;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
	float height = [_delegate heightOfRowInGridView:indexPath.row];
	return (indexPath.row +1 == [_delegate numberOfRowsInGridView])?height + _intervalInCells :height  ;

}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //不让tableviewcell有选中效果
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

-(void)cellClick:(UIGridViewCell *)cell
{
	if ([_delegate respondsToSelector:@selector(didSelectedCellInGrid:)])
	{
		[_delegate didSelectedCellInGrid:cell];
	}
}

- (void)reloadSubViews
{
	[_tableView reloadData];
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
